package com.mayn.jst.shop.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jfinal.plugin.activerecord.Db;
import com.mayn.jst.shop.model.ShopAccountModel;

/**
 * ShopAccount处理类
 * 
 * @author Administrator
 *
 */
public class ShopAccountAction {
	private static final Logger LOG = LoggerFactory.getLogger(ShopAccountAction.class);

	/**
	 * 查询指定店铺帐号登录的cookie
	 * 
	 * @param shopId
	 * @return
	 */
	public static String getShopAccountCookie(int shopId) {
		String cookie = "";
		try {
			String sql = "SELECT SA_COOKIE FROM shop_account WHERE SA_SHOP_ID=?";
			cookie = Db.queryStr(sql, shopId);
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return cookie;
	}
	
	/**
	 * 查询指定店铺帐号登录的cookie
	 * 
	 * @param shopId
	 * @return
	 */
	public static String getShopAccountAlimamaCookie(int shopId) {
		String cookie = "";
		try {
			String sql = "SELECT SA_ALIMAMA_COOKIE FROM shop_account WHERE SA_SHOP_ID=?";
			cookie = Db.queryStr(sql, shopId);
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return cookie;
	}

	/**
	 * 保存指定店铺帐号登陆的cookie
	 * 
	 * @param shopId
	 * @param cookie
	 * @param status
	 * @return
	 */
	public static Boolean saveShopAccountCookie(int shopId, String cookie, int status) {
		try {
			String sql = "UPDATE shop_account SET SA_COOKIE=?,SA_STATUS=?,SA_UPDATE_DATETIME=now() WHERE SA_SHOP_ID=?";
			Db.update(sql, cookie, status, shopId);
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return false;
	}
	
	/**
	 * 保存指定店铺帐号登陆的cookie
	 * 
	 * @param shopId
	 * @param cookie
	 * @param status
	 * @return
	 */
	public static Boolean saveShopAccountAlimamaCookie(int shopId, String cookie, int status) {
		try {
			String sql = "UPDATE shop_account SET SA_ALIMAMA_COOKIE=?,SA_STATUS=?,SA_UPDATE_DATETIME=now() WHERE SA_SHOP_ID=?";
			Db.update(sql, cookie, status, shopId);
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return false;
	}

	/**
	 * 查询指定店铺帐号记录
	 * 
	 * @param shopId
	 * @return
	 */
	public static ShopAccountModel getShopAccount(int shopId) {
		try {
			String sql = "SELECT * FROM shop_account sa INNER JOIN shop s on sa.SA_SHOP_ID=s.SHOP_ID WHERE SA_SHOP_ID=?";
			return ShopAccountModel.dao.findFirst(sql, shopId);
		} catch (Exception e) {
			LOG.error(Thread.currentThread().getStackTrace()[1].getMethodName() + " Exception,Msg=" + e.getMessage());
		}
		return null;
	}

	public static List<ShopAccountModel> getShopAccountList() {
		String sql = "SELECT * FROM shop_account where SA_DEL = 0 ";
		List<ShopAccountModel> shopAccountList = ShopAccountModel.dao.find(sql);
		return shopAccountList;
	}

}
